<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入问卷标题"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="surveyList">
      <el-table-column label="ID" align="center" prop="surveyId" width="80" />
      <el-table-column label="问卷标题" align="center" prop="title" :show-overflow-tooltip="true" />
      <el-table-column label="问卷描述" align="center" prop="description" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="info">待审批</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="提交人" align="center" prop="createBy" width="100" />
      <el-table-column label="提交时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handlePreview(scope.row)"
          >预览</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleApprove(scope.row, '1')"
            v-hasPermi="['system:survey:approval']"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="handleApprove(scope.row, '0')"
            v-hasPermi="['system:survey:approval']"
          >拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 问卷预览对话框 -->
    <el-dialog title="问卷预览" :visible.sync="previewOpen" width="800px" append-to-body>
      <div class="preview-container" v-loading="previewLoading">
        <h2 class="preview-title">{{ previewData.title }}</h2>
        <div class="preview-description">{{ previewData.description }}</div>
        <div class="preview-info">
          <div>开始时间：{{ parseTime(previewData.startTime) }}</div>
          <div>结束时间：{{ parseTime(previewData.endTime) }}</div>
        </div>
        <div class="question-list">
          <div v-if="previewData.questions && previewData.questions.length === 0" class="empty-questions">
            暂无问题，请先添加问题
          </div>
          <div v-else>
            <div class="question-item" v-for="(question, index) in previewData.questions" :key="index">
              <div class="question-title">
                {{ index + 1 }}. {{ question.title }}
                <span class="required" v-if="question.required == 1">*</span>
              </div>
              <div class="question-content" v-if="question.type === 'radio'">
                <el-radio-group disabled>
                  <div v-for="(option, optIndex) in JSON.parse(question.options || '[]')" :key="optIndex">
                    <el-radio :label="optIndex">{{ option.label }}</el-radio>
                  </div>
                </el-radio-group>
              </div>
              <div class="question-content" v-else-if="question.type === 'checkbox'">
                <el-checkbox-group disabled>
                  <div v-for="(option, optIndex) in JSON.parse(question.options || '[]')" :key="optIndex">
                    <el-checkbox :label="optIndex">{{ option.label }}</el-checkbox>
                  </div>
                </el-checkbox-group>
              </div>
              <div class="question-content" v-else-if="question.type === 'text'">
                <el-input type="textarea" disabled placeholder="请输入" />
              </div>
              <div class="question-content" v-else-if="question.type === 'rate'">
                <el-rate disabled />
              </div>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="previewOpen = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 审批对话框 -->
    <el-dialog :title="approvalTitle" :visible.sync="approvalOpen" width="500px" append-to-body>
      <el-form ref="approvalForm" :model="approvalForm" :rules="approvalRules" label-width="80px">
        <el-form-item label="审批意见" prop="opinion">
          <el-input v-model="approvalForm.opinion" type="textarea" placeholder="请输入审批意见" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitApproval">确 定</el-button>
        <el-button @click="approvalOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSurveyQuestionnaire, getSurveyQuestionnaire, approveSurveyQuestionnaire } from "@/api/system/survey";

export default {
  name: "SurveyApproval",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问卷表格数据
      surveyList: [],
      // 预览问卷弹窗
      previewOpen: false,
      // 预览问卷数据
      previewData: {},
      // 预览加载状态
      previewLoading: false,
      // 审批相关
      approvalOpen: false,
      approvalTitle: "",
      approvalForm: {
        surveyId: undefined,
        status: "1", // 默认通过
        opinion: ""
      },
      approvalRules: {
        opinion: [
          { required: true, message: "审批意见不能为空", trigger: "blur" }
        ]
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        status: "1" // 固定查询待审核状态
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询待审批问卷列表 */
    getList() {
      this.loading = true;
      listSurveyQuestionnaire(this.queryParams).then(response => {
        this.surveyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 预览问卷 */
    handlePreview(row) {
      this.previewOpen = true;
      this.previewLoading = true;
      getSurveyQuestionnaire(row.surveyId).then(response => {
        this.previewData = response.data;
        this.previewLoading = false;
      });
    },
    /** 审批操作 */
    handleApprove(row, status) {
      this.approvalForm = {
        surveyId: row.surveyId,
        status: status,
        opinion: ""
      };
      this.approvalTitle = status === "1" ? "审批通过" : "审批拒绝";
      this.approvalOpen = true;
    },
    /** 提交审批 */
    submitApproval() {
      this.$refs["approvalForm"].validate(valid => {
        if (valid) {
          approveSurveyQuestionnaire(this.approvalForm).then(response => {
            this.$modal.msgSuccess(this.approvalForm.status === "1" ? "审批通过成功" : "审批拒绝成功");
            this.approvalOpen = false;
            this.getList();
          });
        }
      });
    }
  }
};
</script>

<style scoped>
.preview-container {
  padding: 20px;
}
.preview-title {
  text-align: center;
  margin-bottom: 15px;
}
.preview-description {
  margin-bottom: 20px;
  color: #666;
  text-align: center;
}
.preview-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  color: #999;
  font-size: 14px;
}
.question-list {
  margin-top: 30px;
}
.question-item {
  margin-bottom: 25px;
  padding-bottom: 15px;
  border-bottom: 1px solid #eee;
}
.question-title {
  font-weight: bold;
  margin-bottom: 10px;
}
.question-content {
  margin-left: 20px;
}
.required {
  color: #f56c6c;
  margin-left: 5px;
}
.empty-questions {
  text-align: center;
  color: #999;
  padding: 30px;
}
</style> 